#
# Copyright 2008 Wepoco.  http://www.wepoco.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp

from StringIO import StringIO
from GPXtoKML import GPXtoKML

from template import header, footer

class MainPage(webapp.RequestHandler):
  def get(self):
    
    self.response.out.write(header(self.request))

    tracks = None

    if self.request.path == "/shared":
      tracks = db.GqlQuery("SELECT * FROM Track WHERE isPublic = TRUE")
      self.response.out.write("""<h3>Shared tracks</h3>""")
      for track in tracks:
        if track.user:
          nickname = track.user.nickname()
        else:
          nickname = '-'
          pass
        try:
          self.response.out.write("""
<div class="msg"><b>%s</b>&nbsp;%s&nbsp;<a href="/kml?key=%s">KML</a>
&nbsp;<a href="/map?key=%s">map</a></div>
""" % (cgi.escape(track.name),track.getStartDatetimeStr(),
       track.key(), track.key()))
        except:
          pass
        pass

    if users.GetCurrentUser():
      tracks = db.GqlQuery("SELECT * FROM Track WHERE user = :1", users.GetCurrentUser() )
      self.response.out.write("""<h3>My tracks</h3>""")
      self.response.out.write("""<form action="/listaction" method="post">""")
      for track in tracks:
        if track.user:
          nickname = track.user.nickname()
        else:
          nickname = '-'
          pass
        try:
          self.response.out.write("""
<div class="msg">
<input type="checkbox" name="a" value="%s" />&nbsp;<a 
href="/photos?key=%s">%s</a>&nbsp;%s&nbsp;<a href="/kml?key=%s">KML</a>
&nbsp;<a href="/map?key=%s">map</a>&nbsp;<a href="/timeline?key=%s">timeline</a>
</div>
""" % (track.key(),track.key(),cgi.escape(track.name),track.getStartDatetimeStr(),
       track.key(), track.key(), track.key()))
        except:
          self.response.out.write("""<div class="msg"><input type="checkbox" name="a" value="%s" /><b>Bad entry - please delete me</b></div>""" % (track.key()))
          pass
        pass # for
      self.response.out.write("""
<select name="act_sel" onchange="act_go('a');">
<option value="">Actions...</option>
<option value="pub">Make public</option>
<option value="pri">Make private</option>
<option value="del">Delete</option>
</select>
<input type="submit" name="act_bu_go" value="Go" />
</form>
<form action="/upload" method="post" enctype="multipart/form-data">
<div><input type="submit" value="Upload track" />
<input type="file" name="file" /></div>
</form>
""")
      pass
    self.response.out.write(footer(self.request))
